package com.hnyfkj.jyindustry.common.utils;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
 * @program: jyindustry
 * @description:redis序列化类
 * @author: zl
 * @create: 2020-07-09 18:05
 **/
public class RedisObjectSerializer implements RedisSerializer<Object> {
	private final Converter<Object, byte[]> serializer = new SerializingConverter();
	private final Converter<byte[], Object> deserializer = new DeserializingConverter();
	static final byte[] EMPTY_ARRAY = new byte[0];

	@Override
	public Object deserialize(final byte[] bytes) {
		if (isEmpty(bytes)) {
			return null;
		}
		try {
			return deserializer.convert(bytes);
		} catch (final Exception ex) {
			throw new SerializationException("Cannot deserialize", ex);
		}
	}

	@Override
	public byte[] serialize(final Object object) {
		if (object == null) {
			return EMPTY_ARRAY;
		}
		try {
			return serializer.convert(object);
		} catch (final Exception ex) {
			return EMPTY_ARRAY;
		}
	}

	private boolean isEmpty(final byte[] data) {
		return (data == null || data.length == 0);
	}
}